cc.Class({
    extends: cc.Component,

    properties: {
        _sprIcon:null,
        _zhuang:null,
        _ready:null,
        _offline:null,
        _lblName:null,
        _lblScore:null,
        _fangzhu:null,
        _koufen:null,
        _voicemsg:null,
        
        _chatBubble:null,
        _emoji:null,
        _lastChatTime:-1,
        
        _userName:"",
        _score:0,
        _dif:false,
       
        _isOffline:false,
        _isReady:false,
        _isZhuang:false,
        _isFangzhu:false,
        _userId:null
    },

    // use this for initialization
    onLoad: function () {
        if(cc.vv == null){
            return
        };
        
        this._sprIcon = this.node.getChildByName("icon");
        if (this._sprIcon) {
            this._sprIcon=this._sprIcon.getComponent("ImageLoader")
        }else{
            this._sprIcon = this.node.getChildByName("touxiang").getChildByName("icon").getComponent("ImageLoader")
        };
        this._lblName = this.node.getChildByName("name").getComponent(cc.Label);
        this._lblScore = this.node.getChildByName("score");
        this._koufen = this.node.getChildByName("koufen");
        this._voicemsg = this.node.getChildByName("voicemsg");
        
        if(this._voicemsg){
            this._voicemsg.active = false
        };
        
        if(this._sprIcon && this._sprIcon.getComponent(cc.Button)){
            cc.vv.utils.addClickEvent(this._sprIcon,this.node,"Seat","onIconClicked") 
        };
        
        
        this._offline = this.node.getChildByName("offline");
       
        this._ready = this.node.getChildByName("ready");
        
        this._zhuang = this.node.getChildByName("zhuang");
        this._fangzhu = this.node.getChildByName("fangzhu");
        
        this._chatBubble = this.node.getChildByName("ChatBubble");
        if(this._chatBubble != null){
            this._chatBubble.active = false     
        };
        
        this._emoji = this.node.getChildByName("emoji");
        if(this._emoji != null){
            this._emoji.active = false
        };
        
        this.refresh();
        
        if(this._sprIcon && this._userId){
            this._sprIcon.setUserID(this._userId)
        }
    },
    
    onIconClicked:function(){
        var iconSprite = this._sprIcon.node.getComponent(cc.Sprite);
        if(this._userId != null && this._userId > 0){
           var seat = cc.vv.gameNetMgr.getSeatByID(this._userId);
            var sex = 0;
            if(cc.vv.baseInfoMap){
                var info = cc.vv.baseInfoMap[this._userId];
                if(info){
                    sex = info.sex
                }                
            };
            cc.vv.userinfoShow.show(seat.name,seat.userid,iconSprite,sex,seat.ip)         
        }
    },
    
    refresh:function(){
        // console.log("refresh");
        if(this._lblName != null){
            // console.log("this._userName",this._userName);
            this._lblName.string = this._userName
        };
        if(this._lblScore != null){
            if (!this._dif) {
                this._lblScore.removeAllChildren();
                var scoreArr=(this._score+"").split("");
                // 正负号添加
                if (this._score>0) {
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    sprite.spriteFrame=cc.vv.mjmgr.getZhengNumSpriteFrameByNum("+");
                    this._lblScore.addChild(spriteNode)
                }else if (this._score<0) {
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    sprite.spriteFrame=cc.vv.mjmgr.getFuNumSpriteFrameByNum("");
                    scoreArr.splice(0,1);
                    this._lblScore.addChild(spriteNode)
                };
                // 具体分数
                for (var i = 0; i < scoreArr.length; i++) {
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    if (this._score>0) {
                        sprite.spriteFrame=cc.vv.mjmgr.getZhengNumSpriteFrameByNum(scoreArr[i]);
                    }else{
                        sprite.spriteFrame=cc.vv.mjmgr.getFuNumSpriteFrameByNum(scoreArr[i]);
                    };
                    this._lblScore.addChild(spriteNode)
                }
            }
        };     
        if(this._offline){
            this._offline.active = this._isOffline && this._userName != ""
        };
        if(this._ready){
            this._ready.active = this._isReady;
            // console.log("cc.vv.gameNetMgr.gamestate",cc.vv.gameNetMgr.gamestate);
            if(cc.vv.gameNetMgr.gamestate=="playing"||cc.vv.gameNetMgr.gamestate=="jiaofen"||cc.vv.gameNetMgr.gamestate=="begin"||cc.vv.gameNetMgr.gamestate=="qiangzhuang"){
                console.log("已经在打牌");
                this._ready.active=false
            }
        };
        if(this._zhuang){
            // console.log("this._isZhuang",this._isZhuang);
            this._zhuang.active = this._isZhuang   
        };
        if(this._fangzhu){
            // console.log("this._isZhuang",this._isZhuang);
            this._fangzhu.active = this._isFangzhu   
        };
        this.node.active = this._userName != null && this._userName != ""
    },
    
    setInfo(name,score,dif){
        // dif:是否用统一数字图片
        // 分数分为数字和图片两种
        console.log("setInfo",name,score,dif);
        this._userName = name;
        this._score = score;
        if(this._score == null){
            this._score = 0
        };
        this._dif=dif;
        if(this._lblScore != null){
            this._lblScore.active = this._score != null
        };
        this.refresh()
    },
    setKoufen(score){
        console.log("setKoufen",score);
        if(this._koufen){
            console.log("有扣分节点");
            if (!score&&score!=0) {
                this._koufen.active=false
            }else{
                this._koufen.active=true;
                var name="";
                if (score>0) {
                    name="ying"
                }else if (score<0) {
                    name="shu"
                }else{
                    name="he"
                };
                var self=this;
                var result=this._koufen.getChildByName("result");
                result.getComponent(cc.Sprite).spriteFrame=cc.vv.mjmgr.getResultSpriteFrameByName(name);
               
                var scoreNode=this._koufen.getChildByName("fen");
                scoreNode.removeAllChildren();
                var scoreArr=(score+"").split("");
                // 正负号添加
                if (score>0) {
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    sprite.spriteFrame=cc.vv.mjmgr.getResultNumSpriteFrameByName("+");
                    scoreNode.addChild(spriteNode)
                }else if (score<0) {
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    sprite.spriteFrame=cc.vv.mjmgr.getResultNumSpriteFrameByName("-");
                    scoreArr.splice(0,1);
                    scoreNode.addChild(spriteNode)
                };
                console.log("扣分scoreArr",scoreArr);
                // 具体分数
                for (var i = 0; i < scoreArr.length; i++) {
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    if (score>0) {
                        sprite.spriteFrame=cc.vv.mjmgr.getResultNumSpriteFrameByName(scoreArr[i]);
                    }else{
                        sprite.spriteFrame=cc.vv.mjmgr.getResultNumSpriteFrameByName("-"+scoreArr[i]);
                    };
                    scoreNode.addChild(spriteNode)
                }
            }
        }
    },
    setZhuang:function(value,isdoScale){
        console.log("setZhuang",value);
        this._isZhuang = value;
        if(this._zhuang){
            if (isdoScale) {
                this._zhuang.active = this._isZhuang;
                this._zhuang.scale=1.5;
                var self=this;
                setTimeout(function(){
                    self.doScale(self._zhuang)
                },500)
            }else{
                this._zhuang.active = this._isZhuang
            }
        }
    },
    setFangzhu:function(value){
        // console.log("setZhuang",value);
        this._isFangzhu = value;
        if(this._fangzhu){
            this._fangzhu.active = this._isFangzhu
        }
    },
    setReady:function(isReady){
        console.log("setReady",isReady);
        this._isReady = isReady;
        if(this._ready){
            // console.log("有人准备");
            this._ready.active = this._isReady;
            // console.log("cc.vv.gameNetMgr.gamestate",cc.vv.gameNetMgr.gamestate);
            if(cc.vv.gameNetMgr.gamestate=="playing"||cc.vv.gameNetMgr.gamestate=="jiaofen"||cc.vv.gameNetMgr.gamestate=="begin"||cc.vv.gameNetMgr.gamestate=="qiangzhuang"){
                this._ready.active=false
            }
        }
    },
    
    setID:function(id){
        // console.log("setID",id);
        var idNode = this.node.getChildByName("id");
        if(idNode){
            var lbl = idNode.getComponent(cc.Label);
            lbl.string = "ID:" + id        
        };
        this._userId = id;
        if(this._sprIcon){
            this._sprIcon.setUserID(id)
        }
    },
    
    setOffline:function(isOffline){
        // console.log("setOffline",isOffline);
        this._isOffline = isOffline;
        if(this._offline){
            this._offline.active = this._isOffline && this._userName != ""
        }
    },
    
    chat:function(content){
        if(this._chatBubble == null || this._emoji == null){
            return
        };
        this._emoji.active = false;
        this._chatBubble.active = true;
        this._chatBubble.getComponent(cc.Label).string = content;
        this._chatBubble.getChildByName("New Label").getComponent(cc.Label).string = content;
        this._lastChatTime = 3
    },
    
    emoji:function(emoji){
        //emoji = JSON.parse(emoji);
        if(this._emoji == null || this._emoji == null){
            return
        };
        // console.log(emoji);
        this._chatBubble.active = false;
        this._emoji.active = true;
        this._emoji.getComponent(cc.Animation).play(emoji);
        this._lastChatTime = 3
    },
    
    voiceMsg:function(show){
        if(this._voicemsg){
            this._voicemsg.active = show
        }
    },
    // 从大到小放大效果
    doScale:function(obj,callback){
        // 全部挂在obj上作为属性避免定时器混乱
        obj.scale=1.5;
        obj.action=cc.scaleTo(0.5, 1);
        obj.runAction(obj.action)
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if(this._lastChatTime > 0){
            this._lastChatTime -= dt;
            if(this._lastChatTime < 0){
                this._chatBubble.active = false;
                this._emoji.active = false;
                this._emoji.getComponent(cc.Animation).stop()
            }
        }
    },
});
